<?php
global $_GPC, $_W;
$GLOBALS['frames'] = $this->getMainMenu();
$branch_list = pdo_getall('yzzc_sun_branch',['uniacid'=>$_W['uniacid']],['id','name']);
$where = "where uniacid =" . $_W['uniacid'];
$keywords = $_GPC['keywords'] ?? '';
if ($keywords) {
    $where .= " and (username LIKE  '%$keywords%' or tel LIKE  '%$keywords%' or ordernum LIKE  '%$keywords%')";
}
$branch_id = $_GPC['branch_id'] ?? '';
if ($branch_id){
    $where.= " and branch_id=".$branch_id;
}
$status = $_GPC['status'] ?? '';
if ($status){
    $where.=' and status='.$status;
}

$type = $_GPC['type'] ?? '';
if ($type){
    $where.=' and type='.$type;
}
$page = max(1, intval($_GPC['page']));
$size = intval($_GPC['psize']) ? intval($_GPC['psize']) : 10;
$sql = 'SELECT * FROM' . tablename('yzzc_sun_branch_payout') . " {$where} ORDER BY createtime DESC LIMIT " . (($page - 1) * $size) . ',' . $size;

$info = pdo_fetchall($sql);
if ($info){
    foreach ($info as $key => $value){
        foreach ($branch_list as $val){
            if ($value['branch_id'] == $val['id']){
                $info[$key]['branch_name'] = $val['name'];
            }
        }
        $info[$key]['createtime'] = date('Y-m-d H:i:s',$value['createtime']);
    }
}
$total = pdo_fetchcolumn("select count(*) from " . tablename("yzzc_sun_branch_income") . " where uniacid =" . $_W['uniacid']);
$pager = pagination($total, $page, $size);
global $_W, $_GPC;

if ($_GPC['op'] == 'delete') {
    $payout_info = pdo_get('yzzc_sun_branch_payout', array('id' => $_GPC['id']));
    $branch_info = pdo_get('yzzc_sun_branch', ['id'=>$payout_info['branch_id']]);
    $res = pdo_delete('yzzc_sun_branch_payout', array('id' => $_GPC['id']));
    if ($res) {
        if ($payout_info['status'] == 1){
            pdo_update('yzzc_sun_branch',['balance' => $branch_info['balance'] + $payout_info['total']],['id'=>$payout_info['branch_id']]);
        }
        message('操作成功', $this->createWebUrl('branch_payout', array()), 'success');
    } else {
        message('操作失败', '', 'error');
    }

}

if ($_GPC['op'] == 'change') {
    $where = ['id' => $_GPC['id']];
    $payout_info = pdo_get('yzzc_sun_branch_payout', $where);
    if ($payout_info['status'] == $_GPC['status']) {
        message('状态错误，修改失败', $this->createWebUrl('branch_payout', array()), 'error');
    }

    $data = ['status' => $_GPC['status']];
    if (isset($_GPC['remark']) && $_GPC['remark']) {
        $data['remark'] = $_GPC['remark'];
    }
    $res = pdo_update('yzzc_sun_branch_payout', $data, $where);
    if ($res) {
        if ($data['status'] == 2){
            //审核拒绝 余额退还
            $branch_info = pdo_get('yzzc_sun_branch', ['id'=>$payout_info['branch_id']]);
            pdo_update('yzzc_sun_branch',['balance' => $branch_info['balance'] + $payout_info['total']],['id'=>$payout_info['branch_id']]);
        }
        message('操作成功', $this->createWebUrl('branch_payout', array()), 'success');
    } else {
        message('操作失败', '', 'error');
    }
}

include $this->template('web/branch_payout');